//
//  MemberListViewController.m
//
//
//  Created by jiezsoft.com on 14-10-10.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.


#import "MemberListViewController.h"
#import "GenerateQRCodeViewController.h"
#import "AddMemberViewController.h"
#import "MJRefresh.h"
#import "TagsCell.h"
#import "MemberListCell.h"
#import "MembershipDetailsViewController.h"
#import "TheLoginViewController.h"
#import "MemberListModel.h"
#import "FilterView.h"
#import "MemberSendMsgViewController.h"
#import "ForgotPasswordViewController.h"
#import "ReabamSingleton.h"
#import "DeleteNull.h"
@interface MemberListViewController ()<MBProgressHUDDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UITextFieldDelegate,UISearchBarDelegate,FilterViewDelegate,MemberListCellDelegate,MembershipDetailsViewControllerDelegate,AddMemberViewControllerControllerDelegate,MemberSendMsgViewControllerDelegate,ForgotPasswordViewControllerDelegate>
@property (strong, nonatomic) NSString*orderField;
@end
@implementation MemberListViewController
{
    UISearchBar *search;
    MJRefreshHeaderView* _header;
    MJRefreshFooterView* _footer;
    NSMutableArray*modelArray;
    NSMutableArray*searchBeans;
    UITableView *table;
    UIButton*screeningBtn;
    int pageNumber;
    MBProgressHUD *progressHud;
    UITextField*searchTextField;
    UIView*tableHeaderView;
    UIView*myToolBar;
    UIButton* screeningBtnImage;
    UIButton* addBtn;
    UIButton*sat;
    UIButton*sat1;
    UIButton*btnNormal;
    UIButton*sat2;
    BOOL saveAccount;
    BOOL saveAccount1;
    NSString* TotalCount;
    UIButton*chooseBtn;
    UIButton*sendMmessageBtn;
    UIButton*chooseBtnImage;
    NSMutableArray*memberIdArray;
    NSMutableArray* memberIdArrays;
    UIView* noDataView;
    UILabel*totalcount;
    ReabamSingleton*commdity;
    NSMutableArray*DataLine;
}
@synthesize alt = alt;
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    self.navigationController.delegate = nil;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    saveAccount=YES;
    saveAccount1=NO;
    commdity=[ReabamSingleton shared];
  
}
-(void)update:(NSString *)Str{
    [self setNavigation];
    [self requestData];
    table .frame=CGRectMake(0, 64, ScreenWidth, ScreenHeight);
}
- (void)secondViewController:(ForgotPasswordViewController *)secondViewController updateMemberPhoneText:(NSString *)memberPhoneText{
    [self setNavigation];
    [self requestData];
    table .frame=CGRectMake(0, 64, ScreenWidth, ScreenHeight);
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.orderField=@"date";
    searchBeans=[[NSMutableArray alloc]init];
      DataLine=[[NSMutableArray alloc]init];
    [self setNavigation];
    [self requestData];
}
- (void)certification:(NSString *)sender{

      MemberListModel *ScreeningModel=modelArray[sender.intValue];
    
    
    ForgotPasswordViewController *detailViewController = [[ForgotPasswordViewController alloc] init];
    detailViewController.memberId=ScreeningModel.memberId;
    detailViewController.userName=ScreeningModel.userName;;
    detailViewController.titleName=@"绑定手机";
    detailViewController.delegate = self;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
- (void)deleteBtn:(NSString *)sender{}
- (void)edit:(NSString *)sender{}
-(void)addoptions:(UIButton *)sender{
    AddMemberViewController *detailViewController = [[AddMemberViewController alloc] init];
    detailViewController.delegate=self;
    [self.navigationController pushViewController:detailViewController animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [searchTextField resignFirstResponder];
    [self requestData];
    return YES;
}
-(void)requestData{
    [totalcount removeFromSuperview];
    [noDataView removeFromSuperview];
    modelArray=[[NSMutableArray alloc]init];
    memberIdArrays=[[NSMutableArray alloc]init];
    pageNumber = 1;
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"1" forKey:@"pageIndex"];
    [param setValue:@"20" forKey:@"pageSize"];
    [param setValue:self.orderField forKey:@"orderField"];
    [param setValue:self.orderSort forKey:@"orderSort"];
    [param setValue:searchTextField.text forKey:@"searchWord"];
    [param setValue:searchBeans forKey:@"searchBeans"];
    if ([self.chooseState isEqualToString:@"选择"]) {
        [param setValue:@"2" forKey:@"filterType"];
    }else{
        [param setValue:@"1" forKey:@"filterType"];
    }
    if (searchBeans .count>0) {
        [screeningBtnImage setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
    }
  
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Business/Member/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
          NSLog(@"====%@",severDataDict.dataDict);
            if (searchBeans.count>0) {
                UIImage *img=[UIImage imageNamed:@"shaixuan_hight"];
                UIImage *tinted = [img rt_tintedImageWithColor:luseColor];
                [screeningBtn setTitleColor:luseColor forState:UIControlStateNormal];
                [screeningBtnImage setImage:tinted forState:UIControlStateNormal];
                
            }else{
                [screeningBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
                [screeningBtnImage setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
            }
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                for (NSDictionary* dicc in [severDataDict.dataDict objectForKey:@"DataLine"] )
                {
//                    if ([[Global getJudgeTheErrorStr:[dicc valueForKey:@"phone"]]isEqualToString:@""]) {
                    if ([dicc valueForKey:@"phone"]==NULL||[[Global getJudgeTheErrorStr:[dicc valueForKey:@"phone"]]isEqualToString:@""]){
                    }else{
                        [memberIdArrays addObject:[dicc valueForKey:@"memberId"]];
                    }
                }
                TotalCount=[severDataDict.dataDict objectForKey:@"TotalCount"];
                if ([self.chooseState isEqualToString:@"选择"]) {
                   
                    totalcount=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 200, 40)];
                    totalcount.text=[NSString stringWithFormat:@"共有 %@ 个结果",TotalCount];
                    totalcount.font=[UIFont systemFontOfSize:14];
                    [tableHeaderView addSubview:totalcount];
                }
                NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                if (mItemsArray.count>0) {
                    for (NSDictionary *dic in mItemsArray) {
                        MemberListModel *dataModel=[MemberListModel dataModelWithDict:[DeleteNull changeType:dic]];
                        [modelArray addObject:dataModel];
                         [ DataLine addObject:dic];
                    }
                }else{
                    noDataView=[[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight/2-50, ScreenWidth, 80)];
                    [self.view addSubview:noDataView];
                    UIImageView*noImage=[[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2-47, 0, 94, 51)];
                    noImage.image=[UIImage imageNamed:@"meiyoushuju"];
                    [noDataView addSubview:noImage];
                    UILabel*noLabel=[[UILabel alloc]initWithFrame:CGRectMake(0,65, ScreenWidth, 14)];
                    noLabel.text=@"没有列表数据，赶紧去添加！";
                    noLabel.textColor=zhonghuiColor; noLabel.font=[UIFont systemFontOfSize:14];
                    noLabel.textAlignment=NSTextAlignmentCenter;
                    [noDataView addSubview:noLabel];
                    
                }
                
                [table reloadData];
            }else{
                [Global hideProgressHUDInternal:self.view];
                if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                    TheLoginViewController*vc1=[[TheLoginViewController alloc] init];
                    [self.navigationController pushViewController:vc1 animated:YES];
                }
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    
    
    [self addTabar];
}

//上拉刷新数据
- (void)addFooter
{
    
    MJRefreshFooterView* footer = [MJRefreshFooterView footer];
    footer.scrollView = table;
    footer.beginRefreshingBlock = ^(MJRefreshBaseView* refreshView)
    {
       
            
        
        if (TotalCount.doubleValue/20.00>pageNumber) {
            pageNumber++;
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *param = [NSMutableDictionary new];
            [param setValue:Token forKey:@"tokenId"];
            [param setValue:[NSString stringWithFormat:@"%d" ,pageNumber] forKey:@"pageIndex"];
            [param setValue:@"20" forKey:@"pageSize"];
            [param setValue:self.orderField forKey:@"orderField"];
            [param setValue:self.orderSort forKey:@"orderSort"];
            [param setValue:searchTextField.text forKey:@"searchWord"];
            [param setValue:searchBeans forKey:@"searchBeans"];
            if ([self.chooseState isEqualToString:@"选择"]) {
                [param setValue:@"2" forKey:@"filterType"];
            }else{
                [param setValue:@"1" forKey:@"filterType"];
            }
            
           // NSLog(@"====%@",param);
            [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
            [HttpRequest requestData:@"Business/Member/List" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                  NSLog(@"====%@",severDataDict.dataDict);
                    

                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                        for (NSDictionary* dicc in [severDataDict.dataDict objectForKey:@"DataLine"] )
                        {
                         if ([dicc valueForKey:@"phone"]==NULL||[[Global getJudgeTheErrorStr:[dicc valueForKey:@"phone"]]isEqualToString:@""]){
                         }else{
                                [memberIdArrays addObject:[dicc valueForKey:@"memberId"]];
                            }
                        }
                        NSMutableArray * mItemsArray=[NSMutableArray arrayWithArray:[severDataDict.dataDict objectForKey:@"DataLine"]];
                      
                        for (NSDictionary *dic in mItemsArray) {
                            MemberListModel *dataModel=[MemberListModel dataModelWithDict:[DeleteNull changeType:dic]];
                            [modelArray addObject:dataModel];
                            [ DataLine addObject:dic];
 
                        }
                        [table reloadData];
                    }else{
                        [Global hideProgressHUDInternal:self.view];
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        [Global showTextHudSub:requestResultStr view:self.view];
                    }
                    
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];
        }else{
            
            NSString *requestResultStr=@"没有更多数据了！";
            [Global showTextHudSub:requestResultStr view:self.view];
        }
        [self performSelector:@selector(doneWithView:) withObject:refreshView afterDelay:3.0f];
        NSLog(@"%@--->正在刷新", refreshView.class);
    };
}
- (void)doneWithView:(MJRefreshBaseView *)refreshView
{
    
    [self addTabar];
    [table reloadData];
    [refreshView endRefreshing];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [modelArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MemberListViewController"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    for (UIView *view in [cell subviews]){
        [view removeFromSuperview];
    }
    if ([self.chooseState isEqualToString:@"选择"]) {
        cell.chooseState=@"选择";
    }else{
        cell.chooseState=@"";
        cell.memberIdArray=[memberIdArray mutableCopy];
    }
    cell.tagStr=[NSString stringWithFormat:@"%ld",  indexPath.row];
    cell.model=modelArray[indexPath.row];
    cell.mDelegate=self;
    
    
    return cell;
}
- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return  90;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (tableView == table) {
        MemberListModel *ScreeningModel=modelArray[indexPath.row];
        if ([self.chooseState isEqualToString:@"选择"]) {
            
            if (DataLine.count>0) {
            NSDictionary*dic=DataLine[indexPath.row];
            commdity.dataMemberDict=dic;
            }

                                [self.delegate memberIdStr:ScreeningModel.memberId userName:ScreeningModel.userName phone:ScreeningModel.phone sex:ScreeningModel.sex  gradeName:ScreeningModel.gradeName consigneeName:ScreeningModel.consigneeName consigneeAddr:ScreeningModel.consigneeAddr consigneePhone:ScreeningModel.consigneePhone mdiscountMoney:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%@",@"0.00"]]];
                                [self.navigationController popViewControllerAnimated:YES];

       
        }else{
            MembershipDetailsViewController *detailViewController = [[MembershipDetailsViewController alloc] init];
            detailViewController.delegate=self;
            detailViewController.memberId=ScreeningModel.memberId;
            [self.navigationController pushViewController:detailViewController animated:YES];
        }
    }
}

-(void)chooseBtn:(NSString *)str{
    if (![memberIdArray containsObject:str]) {
        [memberIdArray addObject:[NSString stringWithFormat:@"%@",str]];
        [self addTabar];
    }else{
        [memberIdArray removeObject:str];
        [self addTabar];
        
    }
}
-(void)chooseBtnImage:(UIButton *)str{
    saveAccount1=!saveAccount1;
    if (saveAccount1) {
        memberIdArray=[[NSMutableArray alloc]init];
        memberIdArray=[memberIdArrays mutableCopy];
        [chooseBtnImage setImage:[UIImage imageNamed:@"gous"] forState:UIControlStateNormal];
        [sendMmessageBtn setTitle:[NSString stringWithFormat:@"发信息（ %ld ） ",(unsigned long)memberIdArray.count] forState:UIControlStateNormal];
        [table reloadData];}
    else
    {
        [memberIdArray removeAllObjects];
        [chooseBtnImage setImage:[UIImage imageNamed:@"quans"] forState:UIControlStateNormal];
        [sendMmessageBtn setTitle:[NSString stringWithFormat:@"发信息（ %ld ） ",(unsigned long)memberIdArray.count] forState:UIControlStateNormal];
        [table reloadData];
    }}
-(void)addTabar{
    if (memberIdArray.count>0) {
        [chooseBtn removeFromSuperview];
        [sendMmessageBtn removeFromSuperview];
        chooseBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, ScreenHeight-48, ScreenWidth-160, 48)];
        [chooseBtn setTitle:@"             全选" forState:UIControlStateNormal];
        chooseBtn.backgroundColor=[UIColor whiteColor];
        [chooseBtn setTitleColor:qianheiColor forState:UIControlStateNormal];
        chooseBtn.alpha=0.8;
        chooseBtn.layer.borderWidth = 0.25;
        chooseBtn.layer.borderColor = qianhuiColor.CGColor;
        [chooseBtn addTarget:self action:@selector(chooseBtnImage:) forControlEvents:UIControlEventTouchUpInside];
        chooseBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentFill;
        chooseBtn.titleLabel.font=[UIFont systemFontOfSize:15];
        [self.view addSubview:chooseBtn];
        chooseBtnImage=[[UIButton alloc]initWithFrame:CGRectMake(15, 48/2-30/2, 30, 30)];
        chooseBtnImage.userInteractionEnabled=NO;
        if (memberIdArray.count==memberIdArrays.count) {
            [chooseBtnImage setImage:[UIImage imageNamed:@"gous"] forState:UIControlStateNormal];
            
        }else{
            saveAccount1=NO;
            [chooseBtnImage setImage:[UIImage imageNamed:@"quans"] forState:UIControlStateNormal];}
        [chooseBtn addSubview:chooseBtnImage];
        sendMmessageBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-160, ScreenHeight-48,160, 48)];
        [sendMmessageBtn setTitle:[NSString stringWithFormat:@"发信息（ %ld ） ",(unsigned long)memberIdArray.count] forState:UIControlStateNormal];
        sendMmessageBtn.backgroundColor=luseColor;
        [sendMmessageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        sendMmessageBtn.titleLabel.font=[UIFont systemFontOfSize:16];
        [sendMmessageBtn addTarget:self action:@selector(sendMmessageBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        sendMmessageBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [self.view addSubview:sendMmessageBtn];
        
    }else{
        
        [chooseBtn removeFromSuperview];
        [sendMmessageBtn removeFromSuperview];
    }
    
}
-(void)sendMmessageBtn:(UIButton*)sender{
    
    
    if (memberIdArray.count<1)
    {
        [self showAlertMessage:@"请先选择要发短信的会员"];
    }
    else{
        
        MemberSendMsgViewController *detailViewController = [[MemberSendMsgViewController alloc] init];
        detailViewController.memberIds=[memberIdArray mutableCopy];
        detailViewController.delegate=self;
        [self.navigationController pushViewController:detailViewController animated:YES];
    }
}
-(void)sat:(UIButton*)sender{
    [sat1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sat setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sat2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    searchBeans=nil;
    self.orderField=@"date";
    [self requestData];
}
-(void)sat1:(UIButton*)sender{
    self.orderField=@"amount";
    [self requestData];
    [sat setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sat1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [sat2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    saveAccount=!saveAccount;
    if (saveAccount) {
        self.orderSort=@"desc";
          [btnNormal setImage:[UIImage imageNamed:@"shang_hight"] forState:UIControlStateNormal];
    }else{
        self.orderSort=@"asc";
         [btnNormal setImage:[UIImage imageNamed:@"xia_hight"] forState:UIControlStateNormal];
    }
}
-(void)sat2:(UIButton*)sender{
    self.orderField=@"active";
    [self requestData];
    [sat setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sat1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [sat2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    saveAccount=!saveAccount;
    if (saveAccount) {
        self.orderSort=@"desc";
    }else{
        self.orderSort=@"asc";
    }
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}
- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
-(void)setNavigation{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    self.navigationController.navigationBarHidden=NO;
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.textAlignment=NSTextAlignmentLeft;
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    UIView* sousuoView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth*570/750, 65/2)];
    sousuoView.backgroundColor=zuiqianhuiColor;
    UIButton *screeningBtnImage1 = [[UIButton alloc] initWithFrame:CGRectMake(2, 4, 50/2, 50/2)];
    [screeningBtnImage1 setImage:[UIImage imageNamed:@"sousuo"] forState:UIControlStateNormal];
    [sousuoView addSubview:screeningBtnImage1];
    searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(screeningBtnImage1.right,1, ScreenWidth*500/750,65/2)];
    [searchTextField setBorderStyle:   UITextBorderStyleNone]; //外框类型
    searchTextField.placeholder = @"会员/手机号/姓名"; //默认显示的字
    searchTextField.font = [UIFont systemFontOfSize: 14];
    searchTextField.tag=22;
    searchTextField.returnKeyType = UIReturnKeyDone;
    searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    searchTextField.delegate = self;
    searchTextField.tintColor =tintColors;
    [sousuoView addSubview:searchTextField];
    
    self.navigationItem.titleView = sousuoView;
    
    addBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 0.0, 60/2, 60/2)];
    [addBtn setImage:[UIImage imageNamed:@"tianjia"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addoptions:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:addBtn];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    myToolBar=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, viewheight)];
    myToolBar.backgroundColor=jianpancolor;
    UIButton*btnDone111=[[UIButton alloc]initWithFrame:CGRectMake(myToolBar.frame.size.width - 55, 12, 40, 16)];
    [btnDone111 setTitle:@"确定" forState:UIControlStateNormal];
    [btnDone111 setTitleColor:jianpancolor1 forState:UIControlStateNormal];
    btnDone111 .titleLabel .font=[UIFont systemFontOfSize:17];
    [btnDone111 addTarget:self action:@selector(OnTapDone) forControlEvents:UIControlEventTouchUpInside];
    [myToolBar addSubview:btnDone111];
    myToolBar.alpha=alp;
    searchTextField.inputAccessoryView = myToolBar;
    table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    table.separatorStyle=UITableViewCellSeparatorStyleNone;
    [table registerClass:[MemberListCell class] forCellReuseIdentifier:@"MemberListViewController"];
    [self.view addSubview:table];
    [self addFooter];
    if ([self.chooseState isEqualToString:@"选择"]) {
        tableHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
        tableHeaderView.backgroundColor=zuiqianhuiColor;
        table.tableHeaderView=tableHeaderView;
    }else{
        
        [self tableHeaderView];
        
    }
    
    memberIdArray=[[NSMutableArray alloc]init];
    
}
-(void)BackAction:(UIButton *)sender{
       [searchTextField resignFirstResponder];
    if ([self.chooseState isEqualToString:@"选择"]) {
        [self.navigationController popViewControllerAnimated:YES];//选择会员的界面自动退回上一层
    }else{
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];//会员列表的界面自动退回首页
    }
 }
-(void)OnTapDone{
    [searchTextField resignFirstResponder];
}
-(void)tableHeaderView{
    
    tableHeaderView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    tableHeaderView.backgroundColor=zuiqianhuiColor;
    table.tableHeaderView=tableHeaderView;
    
    sat=[[UIButton alloc]initWithFrame:CGRectMake(18, 0, ScreenWidth/4, 40)];
    [sat setTitle:@"最新" forState:UIControlStateNormal];
    [sat setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    sat.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    sat.titleLabel.font=[UIFont systemFontOfSize:ScreeningFont];
    [sat addTarget:self action:@selector(sat:) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:sat];
    
    
    sat1=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3-20, 0, ScreenWidth/4, 40)];
    [sat1 setTitle:@"成交额" forState:UIControlStateNormal];
    [sat1 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    sat1.titleLabel.font=[UIFont systemFontOfSize:ScreeningFont];
    sat1.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [sat1 addTarget:self action:@selector(sat1:) forControlEvents:UIControlEventTouchUpInside];
    sat1.titleLabel.textAlignment=NSTextAlignmentCenter;
    [tableHeaderView addSubview:sat1];
    btnNormal=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3+27.5, 15, 10, 10)];
    [btnNormal setImage:[UIImage imageNamed:@"jiantou_normal"] forState:UIControlStateNormal];
    [btnNormal addTarget:self action:@selector(sat1:) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:btnNormal];
    
    sat2=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth/3*2-30, 0, ScreenWidth/4, 40)];
    [sat2 setTitle:@"活跃度" forState:UIControlStateNormal];
    sat2.titleLabel.font=[UIFont systemFontOfSize:ScreeningFont];
    [sat2 setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    sat2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
    [sat2 addTarget:self action:@selector(sat2:) forControlEvents:UIControlEventTouchUpInside];
    sat2.titleLabel.textAlignment=NSTextAlignmentCenter;
    [tableHeaderView addSubview:sat2];
    
    screeningBtn=[[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-78,  0, 60,40)];
    [screeningBtn.layer setCornerRadius:2.0];
    [screeningBtn.layer setBorderWidth:0];
    [screeningBtn.layer setMasksToBounds:YES];
    [screeningBtn addTarget:self action:@selector(sxuan:) forControlEvents:UIControlEventTouchUpInside];
    CGColorSpaceRef colorSpace2 = CGColorSpaceCreateDeviceRGB();
    CGColorRef colorref2 = CGColorCreate(colorSpace2,(CGFloat[]){153/255.0,153/255.0, 153/255.0, 1.0   });
    [screeningBtn.layer setBorderColor:colorref2];//边框颜色
    [screeningBtn setTitle:@"筛选" forState:UIControlStateNormal];
    screeningBtn.titleLabel.font=[UIFont systemFontOfSize:14];
    [screeningBtn setTitleColor:shenhuiColor forState:UIControlStateNormal];
    screeningBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentRight;
    [tableHeaderView addSubview:screeningBtn];
    screeningBtnImage = [[UIButton alloc] initWithFrame:CGRectMake(3.5,5, 30, 30)];
    [screeningBtnImage setImage:[UIImage imageNamed:@"shaixuan"] forState:UIControlStateNormal];
    [screeningBtnImage addTarget:self action:@selector(sxuan:) forControlEvents:UIControlEventTouchUpInside];
    [screeningBtn addSubview:screeningBtnImage];
   UIView* tableFooterView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    tableFooterView.backgroundColor=[UIColor clearColor];
    table.tableFooterView=tableFooterView;
}
-(void)sxuan:(UIButton*)sender{
    
    FilterView *filterView=[FilterView FilterViewWithSize:CGSizeMake(ScreenWidth, ScreenHeight)];
    filterView.typeStr=@"member";
    filterView.delegate=self;
    filterView.selectedSearchBeansArray=searchBeans;
    [filterView show];
    
}
#pragma mark FilterViewDelegate
-(void)confirmFilter:(NSMutableArray *)searchBeansArray
{
//    NSLog(@"--%@",searchBeansArray);
    searchBeans=searchBeansArray;
    [self update:@""];
}
-(void)cancelPop
{
}

@end
